﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------


using GameFramework.Event;
using UnityEngine;
using UnityGameFramework.Runtime;
using Log = UnityGameFramework.Runtime.Log;

namespace Cheng
{
    public abstract class GameBase
    {
        public abstract GameMode GameMode
        {
            get;
        }

       
        public bool GameOver
        {
            get;
            protected set;
        }

        //private Attribute m_Player = null;

        public virtual void Initialize()
        {
            GameEntry.Event.Subscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
            GameEntry.Event.Subscribe(ShowEntityFailureEventArgs.EventId, OnShowEntityFailure);
          
            //if (/*!PlayerManagr.Instance.setAttribute*/m_Player==null)
            {
                //GameEntry.Entity.ShowPlayer(new MyAttributeData(GameEntry.Entity.GenerateSerialId(), 10000, CampType.Player)
                //{
                //    Name = "Player",
                //    Position = new Vector3(0.7f, -13.05f),
                //    Scale = Vector3.one * 0.3f,
                //});
            }

            //GameEntry.Entity.ShowNPC(new AttributeData(GameEntry.Entity.GenerateSerialId(), 10001, CampType.Neutral)
            //{
            //    Name = "Hunter",
            //    Position = new Vector3(-5f, -13f),
            //    Scale = Vector3.one * 0.6f,
            //});

            GameOver = false;
        }

       
        public virtual void Shutdown()
        {
            GameEntry.Event.Unsubscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
            GameEntry.Event.Unsubscribe(ShowEntityFailureEventArgs.EventId, OnShowEntityFailure);
        }

        public virtual void Update(float elapseSeconds, float realElapseSeconds)
        {
            //if (m_MyAircraft != null && m_MyAircraft.IsDead)
            //{
            //    GameOver = true;
            //    return;
            //}
        }

        protected virtual void OnShowEntitySuccess(object sender, GameEventArgs e)
        {
            ShowEntitySuccessEventArgs ne = (ShowEntitySuccessEventArgs)e;

            //这里将属性发送到技能面板
            //if (ne.EntityLogicType == typeof(MyAttribute))
            //{
            //    if (((GameObject)ne.Entity.Handle).CompareTag("Player"))
            //    {
            //        m_Player = (MyAttribute)ne.Entity.Logic;
                    
            //        GameEntry.Event.Fire(this, CustomBaseEventArgs.Create("SetVirtualCamera", ((GameObject)ne.Entity.Handle).transform));
            //    }
               
            //}
            //else if(ne.EntityLogicType == typeof(Skill))
            //{

            //    var skill = ((Skill)ne.Entity.Logic);
            //    var game = ((GameObject)ne.Entity.Handle);
            //    var abilityItem = EGamePlay.Entity.Create<AbilityItem>(skill.SkillData.Execution);
            //    abilityItem.Position = game.transform.position;
            //    game.GetComponentInChildren<AbilityItemProxyObj>(true).AbilityItem = abilityItem;
            //    var collider2D = game.GetComponentInChildren<Collider2D>(true);
            //    collider2D.enabled = false;
            //    ((Skill)ne.Entity.Logic).OnTriggerEnterCallbackAction = (other, obj) =>
            //     {
            //         CombatEntity combatEntity = null;

            //         if (CombatContext.Instance.Object2Entities.TryGetValue(other, out combatEntity))
            //         {
            //             abilityItem.OnCollision(combatEntity);
            //         }
            //     };

            //    collider2D.enabled = true;

            //    abilityItem.AddComponent<MoveWithDotweenComponent>().DoMoveTo(skill.SkillData.Execution.InputTarget==null?abilityItem.Position + (Mathf.Abs(skill.SkillData.Direction) == 1f ? Vector3.right : Vector3.up * -0.5f) * 3 * skill.SkillData.Direction: skill.SkillData.Execution.InputTarget.Position, 0.5f).OnMoveFinish(() => { EGamePlay.Entity.Destroy(abilityItem); });

            //}


        }

        protected virtual void OnShowEntityFailure(object sender, GameEventArgs e)
        {
            ShowEntityFailureEventArgs ne = (ShowEntityFailureEventArgs)e;
            Log.Warning("Show entity failure with error message '{0}'.", ne.ErrorMessage);
        }
    }
}
